from model.BaseModel import *
import pandas as pd

class ExamModel(BaseModel):
    def __init__(self):
        super(ExamModel, self).__init__("sto")

    def getExamPaper(self,sid):
        options = sid.split("_")
        tablename = "questions_"+"_".join(options[0:-1])
        print(tablename)
        sql = "select * from " + tablename + " limit 1;"
        rs = self.db.executeSql(sql)
        if rs == False:
            sql = ("select q.qid,q.qtype,q.title,q.content,q.answer,q.other_comment,epq.big_ord,epq.small_ord,epq.score "
               "from exam_paper_question epq left join questions q on epq.qid=q.qid "
               "where sid='" + str(sid) + "' order by big_ord,small_ord;")
            return self.db.executeSqlMapByFields(sql,["qid","qtype","title","content","answer","other_comment","big_ord","small_ord","score"])
        else:
            sql = ("select q.qid,q.qtype,q.title,q.content,q.answer,q.other_comment,epq.big_ord,epq.small_ord,epq.score "
                   "from exam_paper_question epq left join " + tablename + " q on epq.qid=q.qid "
                   "where sid='" + str(sid) + "' order by big_ord,small_ord;")
            return self.db.executeSqlMapByFields(sql,
                                                 ["qid", "qtype", "title", "content", "answer", "other_comment","big_ord", "small_ord",
                                                  "score"])

    def getRandomPaper(self,qtype,fetchnum):
        sql = "select * from questions_jiashizheng_c1 where qtype='"+ qtype +"' order by rand() limit " + str(fetchnum)
        return self.db.executeSqlMapByFields(sql, ["id","qid", "question_group_id","question_group_name",
                                                   "qtype", "title", "content", "answer","explains","other_comment","ctime","mtime"])

    def getAllQuestions(self,tablename):
        sql = "select * from " + tablename
        return self.db.executeSqlMapByFields(sql, ["id", "qid", "question_group_id", "question_group_name",
                                                   "qtype", "title", "content", "answer", "explains", "other_comment",
                                                   "ctime", "mtime"])